package com.cqgc.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqgc.entity.User;
import com.cqgc.mapper.UserMapper;
import com.cqgc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    ////根据状态和角色查询全部用户信息，在地址上加state=？&role=？
    @Override
    public Page<User> findUsersByStatus(Integer state, Integer roleFromToken, Integer role, com.cqgc.entity.Page page) {
        Page<User> page1 = new Page<>(page.getCurrentPage(), page.getPageSize());
        //输入state等于数据库state条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (roleFromToken == 1) {
            if (role != 0) {
                queryWrapper.eq("role", role);
            }
            if (state != 0) {
                queryWrapper.eq("state", state);
            }

        } else {
            if (role == 2 || role == 3) {
                queryWrapper.eq("role", role);

            }
            queryWrapper.ne("role", 1);
            queryWrapper.eq("state", 1);
        }
        Page<User> userPage = userMapper.selectPage(page1, queryWrapper);

        return userPage;
    }

    //通过id修改用户信息
    @Override
    public boolean updateById(User user) {
        // 先查询原有的用户对象
        User originalUser = userMapper.selectById(user.getUid());

        if (originalUser == null) {
            return false; // 用户不存在
        }
        // 判断传入的属性值是否为空，如果为空则保持原有属性不变
        if (user.getRole() != 0) {
            originalUser.setRole(user.getRole());
        }
        if (!user.getAccount().equals("")) {
            originalUser.setAccount(user.getAccount());
        }
        if (!user.getPassword().equals("")) {
            originalUser.setPassword(user.getPassword());
        }
        if (user.getState() != 0) {
            originalUser.setState(user.getState());
        }
        if (!user.getPhone().equals("")) {
            originalUser.setPhone(user.getPhone());
        }
        if (!user.getUsername().equals("")) {
            originalUser.setUsername(user.getUsername());
        }
        if (!user.getAddress().equals("")) {
            originalUser.setAddress(user.getAddress());
        }
        userMapper.updateById(originalUser);

        // 更新用户信息;
        return true;
    }


    //批量删除
    @Override
    public boolean batchUpdateState(int[] id) {


        UpdateWrapper<User> updateWrapper = Wrappers.update();
        updateWrapper.in("uid", id)
                .eq("state", 1)
                .set("state", -1)
                .or()
                .eq("state", 1)
                .set("state", -1);

        int result = userMapper.update(null, updateWrapper);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    //通过id修改状态，修改state=-1or1
    @Override
    public boolean deleteById(Integer uid) {

        User user = userMapper.selectById(uid);

        if (user != null) {
            if (user.getState() == 1) {
                user.setState(-1);
            } else {
                user.setState(1);
            }
            userMapper.updateById(user);
            return true;
        }
        return false;
    }

    @Override
    public User findByAccount(String account) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("account", account);

        User user = userMapper.selectOne(queryWrapper);
        return user;
    }


    //根据姓名查询-精确
    @Override
    public User findByName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username); // 等于 username 的条
        User user = userMapper.selectOne(queryWrapper);

        return user;
    }


    //根据姓名查询-模糊
    @Override
    public Page<User> findByUsername(String username, com.cqgc.entity.Page page,int role) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", username); // 等于 username 的条
        Page<User> page1 = new Page<>(page.getCurrentPage(), page.getPageSize());
        if (role!=1){
            queryWrapper.ne("role",1);
        }
        Page<User> userPage = userMapper.selectPage(page1, queryWrapper);

        return userPage;
    }


    @Override
    public User checkLogin(String account, String password) {
        return userMapper.getUserByAccountAndPassword(account, password);
    }


    @Override
    public int getRoleByAccount(String account) {
        return userMapper.getRoleByAccount(account).getRole();
    }

    @Override
    public int addUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public User getUserByAccount(String account) {
        return userMapper.getUserByAccount(account);
    }

}
